//
//  HomeViewController.m
//  GoodWayClient
//
//  Created by HuiDong on 15/12/3.
//  Copyright © 2015年 HuiDong. All rights reserved.
//

#import "HomeViewController.h"

@interface HomeViewController ()

@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

//最新广告
@property (weak, nonatomic) IBOutlet UILabel *advertisement;

@property (nonatomic, assign)NSInteger picWidth;

@property (nonatomic, assign)NSInteger picHeight;

@property (nonatomic, assign)NSInteger picNum;

@property (nonatomic, retain)NSTimer *timer;

@property (nonatomic, retain)NSTimer *advTimer;

@property (nonatomic, assign)int count;

@property (nonatomic, strong)NSArray *array;

//账户金额点击事件
- (IBAction)accountMoney:(UIButton *)sender;
//积分账户点击事件
- (IBAction)integral:(UIButton *)sender;
//体验券点击事件
- (IBAction)experienceVoucher:(UIButton *)sender;
//等待发货点击事件
- (IBAction)waitDelivery:(UIButton *)sender;
//进入商城点击事件
- (IBAction)enterShop:(id)sender;


@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.picWidth=375;
    self.picHeight=200;
    self.picNum=5;
    
    for (int i=0; i<self.picNum; i++) {
        CGFloat px=i*self.picWidth;
        
        NSString *path=[NSString stringWithFormat:@"%d.jpg",i+1];
        
        UIImageView *pic=[[UIImageView alloc]initWithFrame:CGRectMake(px, 0, self.picWidth, self.picHeight)];
        
        pic.image=[UIImage imageNamed:path];
        
        [self.myScrollView addSubview:pic];
        
    }
    
    self.myScrollView.contentSize=CGSizeMake(self.picNum*self.picWidth, 0);
    
    self.myScrollView.pagingEnabled=YES;
    
    self.pageControl.numberOfPages=self.picNum;
    
    [self startTimer];
    
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    //开启横条广告轮播
    [self start];
}

/**
 *  懒加载
 *
 *  @return array
 */
-(NSArray *)array
{
    if (!_array) {
        _array = @[@"广告1",@"广告2",@"广告3",@"广告4",@"广告5"];
    }
    return _array;
}


/**
 *  开启定时器
 */
- (void)startTimer{
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(changePic) userInfo:@"" repeats:YES];
}
/**
 *  定时器每秒触发 切换图片
 */
- (void)changePic{
    NSInteger index=self.pageControl.currentPage;
    
    if (index==self.picNum-1) {
        //        self.scrollerView.contentOffset=CGPointZero;
        [self.myScrollView setContentOffset:CGPointZero animated:YES];
        
    }
    else {
        index++;
        //self.scrollerView.contentOffset=CGPointMake(self.picWidth*index,0);
        
        [self.myScrollView setContentOffset: CGPointMake(self.picWidth*index,0)animated:YES];//添加动画
    }
    
}
/**
 *  移除定时器
 */
- (void)removeMyTimer{
    if (self.timer!=nil) {
        [self.timer invalidate];
        self.timer=nil;
    }
}
/**
 *  滑动scorllView触发
 *
 *
 */

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat width=scrollView.contentOffset.x;
    
    self.pageControl.currentPage=width/self.picWidth;
    
}

/**
 *  开始拖拽时移除定时器
 *
 *  @param scrollView
 */
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self removeMyTimer];
}
/**
 *
 *停止拖拽时启动定时器
 */
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    [self startTimer];
}


/**
 *  隐藏状态栏
 */
- (BOOL)prefersStatusBarHidden {
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)accountMoney:(UIButton *)sender {
}

- (IBAction)integral:(UIButton *)sender {
}

- (IBAction)experienceVoucher:(id)sender {
}

- (IBAction)waitDelivery:(UIButton *)sender {
}

- (IBAction)enterShop:(id)sender {
}

- (void)timeChange
{
    if (self.count == (self.array.count-1)) {
        self.count = 0;
    }else{
    self.count++;
    }
    self.advertisement.text = self.array[self.count];
}

- (void)start
{
    //判断是否有正在运行的定时器，防止生成多个定时器同时运行
    if (self.advTimer) {
        [self.advTimer invalidate];
        self.advTimer = nil;
    }
    self.advTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
}
@end
